using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace A_stain_on_the_planet
{
    public class SeagullLevel
    {
        Texture2D m_texBackground;
        Texture2D m_texBackgroundFront;
        Texture2D m_texSeaDirty;
        Texture2D m_texOil;
        Texture2D m_texSea;
        EventHandler m_Eventhandler;
        int m_OilFrameRate = 0;
        int m_OilFrameCount = 0;
        List<Seagull> m_SeagullList ;
        System.Random generator = new System.Random();
        Vector2 m_SpawnPos;
        Texture2D m_texSeagull1;
        Texture2D m_texSeagull2;
        Texture2D m_texSeagull3;

        Texture2D m_texOilSeagull;
        Texture2D m_TexScore;
        Texture2D m_TexTimer;
        Game1 m_game;
        Rectangle m_rect;
        ContentManager m_content;
        int m_NumberOfSeagulsSaved = 0;
        int m_NumberOfDeadSeagulls = 0;
        int m_MaxNumberSeaguls = 0;
        SpriteFont Font1;
        float m_GameTime;
        float m_GameTimeTien;
        float m_Seconden;
        float m_SecondenTien;
        float m_Minuten;
        float m_FyingSpawner;

        public SeagullLevel(Game1 game, int MaxSeagulls)
       {
           m_game = game;
           m_MaxNumberSeaguls = MaxSeagulls;
       }

        public void Initialize(ContentManager content, EventHandler eventhandler)
        {
            m_Eventhandler = eventhandler;
            m_content = content;

            m_texSeagull1 = content.Load<Texture2D>(@"MainCharacters\SeagullAnimatedSprite_final_1-5");
            m_texSeagull2 = content.Load<Texture2D>(@"MainCharacters\SeagullAnimatedSprite_final_1-5_Recolor1");
            m_texSeagull3 = content.Load<Texture2D>(@"MainCharacters\SeagullAnimatedSprite_final_1-5_Recolor2");

            int rand = generator.Next(1, 3);
            if (rand == 1) m_texBackground = content.Load<Texture2D>(@"Backgrounds\V2\BG_ClearSky2_V2_50procent");//content.Load<Texture2D>(@"Backgrounds\V1\BG_ClearSky1");
            else if (rand == 2) m_texBackground = content.Load<Texture2D>(@"Backgrounds\V2\BG_ClearSky2_V2_75procent");//content.Load<Texture2D>(@"Backgrounds\V1\BG_ClearSky2");
            else if (rand == 3) m_texBackground = content.Load<Texture2D>(@"Backgrounds\V1\BG_Sunrise");
            else m_texBackground = content.Load<Texture2D>(@"Backgrounds\V1\BG_Sunset");

            m_texBackgroundFront = content.Load<Texture2D>(@"Backgrounds\V2\Detail_OilTanker_v2");//content.Load<Texture2D>(@"Backgrounds\V1\Detail_OilTanker");

            m_texSeaDirty = content.Load<Texture2D>(@"Backgrounds\V1\Water_DirtTiled");
            m_texSea = content.Load<Texture2D>(@"Backgrounds\V1\Water_WaterTiled");
            m_texOil = content.Load<Texture2D>(@"Backgrounds\V2\Float_OilTiledAnimatedSprite_V3");

            Font1 = content.Load<SpriteFont>("Times New Roman");
            m_texOilSeagull = content.Load<Texture2D>(@"MainCharacters\Stuck_OilAnimatedSprite_All_V2");
            m_TexScore = content.Load<Texture2D>(@"Backgrounds\INT2_Scores_Sprites");
            m_TexTimer = content.Load<Texture2D>(@"Backgrounds\TimerOval");
           
            m_rect = new Rectangle(0, 0, 800, 500);

            m_SeagullList = new List<Seagull>();
            m_SpawnPos = new Vector2(0, 0);
            m_SpawnPos.Y = 370;
            m_SpawnPos.X = 800;
            SpawnSeagull();

            m_SpawnPos.X = 950;
            SpawnSeagull();

            m_SpawnPos.X = 1100;
            SpawnSeagull();

            m_SpawnPos.X = 1270;
            SpawnSeagull();

            m_SpawnPos.X = 1700;
            SpawnSeagull();

            m_SpawnPos.X = 1400;
            SpawnSeagull();
           
            m_SpawnPos.X = 1560;
            SpawnSeagull();

            m_SeagullList.ForEach(delegate(Seagull seagull)
            {
                seagull.Initialize(content);
            });
        }

        private void SpawnSeagull()
        {
            Seagull newseagull = null;
            bool left;

            int rand = generator.Next(1, 3);
            if (rand == 1) left = true;
            else left = false;

            int framegenerated = generator.Next(0, 9);

            int frame = 5;

            frame = framegenerated;

            m_SpawnPos.X = 1400;
            int random = generator.Next(3);
            switch (random)
            {
                case 0:
                    newseagull = new Seagull(this, m_SpawnPos, m_texSeagull1, m_texOilSeagull, left, frame, m_Eventhandler);
                    break;
                case 1:
                    newseagull = new Seagull(this, m_SpawnPos, m_texSeagull2, m_texOilSeagull, left, frame, m_Eventhandler);
                    break;
                case 2:
                    newseagull = new Seagull(this, m_SpawnPos, m_texSeagull3, m_texOilSeagull, left, frame, m_Eventhandler);
                    break;
            }

            m_SeagullList.Add(newseagull);
        }


        public void Draw(GameTime gametime, SpriteBatch spritebatch)
        {

            spritebatch.Draw(m_texBackground, m_rect, Color.White);
            spritebatch.Draw(m_texBackgroundFront, new Vector2(800 - m_texBackgroundFront.Width, 650 - m_texBackgroundFront.Height) , Color.White);

            spritebatch.Draw(m_TexScore, new Rectangle(40, 10, m_TexScore.Width / 6, m_TexScore.Height / 4)
                , new Rectangle(0, 0, m_TexScore.Width / 3, m_TexScore.Height / 2), Color.White);

            string output = " " + m_NumberOfSeagulsSaved.ToString() + "/15";

            Vector2 FontOrigin = Font1.MeasureString(output) / 2;
            FontOrigin.X -= 150;

            spritebatch.DrawString(Font1, output, new Vector2(-20, 35), Color.Black, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);

            spritebatch.Draw(m_TexScore, new Rectangle(40, 35, m_TexScore.Width / 6, m_TexScore.Height / 4)
               , new Rectangle(0, m_TexScore.Height / 2, m_TexScore.Width / 3, m_TexScore.Height / 2), Color.White);

            output = "  " + m_NumberOfDeadSeagulls.ToString() + "/5";

            FontOrigin = Font1.MeasureString(output) / 2;
            FontOrigin.X -= 150;

            spritebatch.DrawString(Font1, output, new Vector2(-20, 70), Color.Black, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);

            spritebatch.Draw(m_TexTimer, new Rectangle(530, 55, (int)(m_TexTimer.Width/1.5f), (int)(m_TexTimer.Height/1.5f)), Color.White);

            output = m_Minuten + ":" + m_SecondenTien + m_Seconden;

            if ((m_GameTime / 10) >= 10)
            {
                m_GameTime = 0;
                ++m_GameTimeTien;

                if (m_GameTimeTien >= 10)
                {
                    m_GameTimeTien = 0;
                    ++m_Seconden;

                    if (m_Seconden >= 10)
                    {
                        m_Seconden = 0;
                        ++m_SecondenTien;

                        if (m_SecondenTien >= 6)
                        {
                            m_SecondenTien = 0;
                            ++m_Minuten;
                        }
                    }
                }
            }

            FontOrigin = Font1.MeasureString(output) / 2;
            FontOrigin.X -= 150;

            spritebatch.DrawString(Font1, output, new Vector2(420, 74), Color.White, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);

            m_SeagullList.ForEach(delegate(Seagull seagull)
            {
                seagull.Draw(gametime, spritebatch);
            });

            
            
        }

        public void AfterDraw(GameTime gametime, SpriteBatch spritebatch)
        {
            spritebatch.Draw(m_texSea, new Vector2(0, 500 - m_texSea.Height + 10), Color.White);
            spritebatch.Draw(m_texSeaDirty, new Vector2(0, 500 - m_texSea.Height + 10), Color.White);

            spritebatch.Draw(m_texOil, new Rectangle(0, 500 - m_texOil.Height / 18 + 10, 800, m_texOil.Height / 18), new Rectangle(0, (m_texOil.Height / 18) * m_OilFrameRate, 800, m_texOil.Height / 18), Color.White);
           

            m_SeagullList.ForEach(delegate(Seagull seagull)
            {
                seagull.AfterDraw(gametime, spritebatch);
            });
        }


        public void Update(GameTime gametime)
        {
            m_GameTime += gametime.ElapsedGameTime.Milliseconds;

            ++m_OilFrameCount;

            if (m_OilFrameCount >= 5)
            {
                m_OilFrameRate++;
                if (m_OilFrameRate >= 18) m_OilFrameRate = 0;
                m_OilFrameCount = 0;
            }


            if (m_SeagullList.Count < m_MaxNumberSeaguls)
            {
                int numbertospawn = m_MaxNumberSeaguls - m_SeagullList.Count;

                for (int i = 0; numbertospawn > i; ++i)
                {
                    int rand = generator.Next(700, 1400);
                    m_SpawnPos.X = rand;
                    m_SpawnPos.Y = 370;

                    bool left = false;

                    int rand2 = generator.Next(1, 3);
                    if (rand2 == 1) left = true;
                    else left = false;

                    int rand3 = generator.Next(0, 9);

                    Seagull newseagull = new Seagull(this, m_SpawnPos, m_texSeagull1, m_texOilSeagull, left, rand3, m_Eventhandler);

                    int random = generator.Next(3);
                    switch (random)
                    {
                        case 0:
                            newseagull = new Seagull(this, m_SpawnPos, m_texSeagull1, m_texOilSeagull, left, rand3, m_Eventhandler);
                            break;
                        case 1:
                            newseagull = new Seagull(this, m_SpawnPos, m_texSeagull2, m_texOilSeagull, left, rand3, m_Eventhandler);
                            break;
                        case 2:
                            newseagull = new Seagull(this, m_SpawnPos, m_texSeagull3, m_texOilSeagull, left, rand3, m_Eventhandler);
                            break;
                    }

                    newseagull.Initialize(m_content);
                    m_SeagullList.Add(newseagull);

                    m_SeagullList.ForEach(delegate(Seagull seagull)
                    {
                        if (seagull.Boundingbox.Intersects(newseagull.Boundingbox) && seagull != newseagull)
                        {
                            m_SeagullList.Remove(newseagull);
                        }
                    });    
                }
            }

            m_FyingSpawner += gametime.ElapsedGameTime.Milliseconds;

            if (m_FyingSpawner >= 14000)
            {
                Seagull newseagull = new Seagull(this, new Vector2(-100, 0), m_texSeagull1, m_texOilSeagull, false, 0, m_Eventhandler);
                int random = generator.Next(3);

                switch (random)
                {
                    case 0:
                        newseagull = new Seagull(this, new Vector2(-100, 0), m_texSeagull1, m_texOilSeagull, false, 0, m_Eventhandler);
                        break;
                    case 1:
                        newseagull = new Seagull(this, new Vector2(-100, 0), m_texSeagull2, m_texOilSeagull, false, 0, m_Eventhandler);
                        break;
                    case 2:
                        newseagull = new Seagull(this, new Vector2(-100, 0), m_texSeagull3, m_texOilSeagull, false, 0, m_Eventhandler);
                        break;
                }

                newseagull.Initialize(m_content);
                newseagull.IsFlyingIn = true;
                newseagull.Velocity = new Vector2(7, -3);
                m_SeagullList.Add(newseagull);

                m_FyingSpawner = 0;
            }

            m_SeagullList.ForEach(delegate(Seagull seagull)
            {
                seagull.Update(gametime);
            });
        }

        public void RemoveSeagull(Seagull DeadSeagull, bool saved)
        {
            m_Eventhandler.SFXSeagullcry();

            if (saved == true)
            {
                m_NumberOfSeagulsSaved++;
                if (m_NumberOfSeagulsSaved >= 15) m_game.Victory();

                m_Eventhandler.SeagullFreed((int)DeadSeagull.Position.X, (int)DeadSeagull.Position.Y);

                m_game.ChangeScore(100);
            }

            else
            {
                m_game.ChangeScore(-50);
                m_NumberOfDeadSeagulls++;

                m_Eventhandler.SeagullLost((int)DeadSeagull.Position.X, (int)DeadSeagull.Position.Y);

                if (m_NumberOfDeadSeagulls >= 5) m_game.Victory();
            }

            m_SeagullList.Remove(DeadSeagull);
        }

        public List<Seagull> GetSeagullList
        {
            get { return m_SeagullList; }
        }

        public void ScoreAdded()
        { 
        
        }

        public float Time
        {
            get { return m_GameTime + (m_GameTimeTien * 100) + (m_Seconden * 1000) + (m_SecondenTien * 10000) + (m_Minuten * 100000); }
        }

        public float deadSeagulls
        {
            get { return m_NumberOfDeadSeagulls; }
        }

        public void draw3D(GameTime gametime, GraphicsDeviceManager graphics)
        {
            m_SeagullList.ForEach(delegate(Seagull fish)
            {
                fish.Draw3D(gametime, graphics);
            });
        }
    }

 
}
